package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:PayController
 * Package:com.atguigu.cloud.controller
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/6/5 - 9:55
 * @Version:v1.0
 */

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法，json串当参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }

    //id是一个必须的路径变量
    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary="删除",description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){
        int ret = payService.delete(id);
        return ResultData.success(ret);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "更新",description="更新支付流水方法，json串当参数")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO,pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录"+i);
    }


    //通过id获得
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "查找",description = "查找支付流水方法，根据id")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){

        if(id==-4) throw new RuntimeException("id不能为负数");

        return ResultData.success(payService.getById(id));
    }

    //获得所有的记录
    @GetMapping("/pay/get")
    @Operation(summary = "查找",description = "查找支付流水方法，查找所有")
    public ResultData<List<Pay>> getAll(){
        return ResultData.success(payService.getAll());
    }

    @Value("${server.port}")
    private String port;
    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(@Value("{${atguigu.info}}")String atguiguInfo){
        return "atguiguInfo: "+atguiguInfo+"\t"+"port: "+port;
    }
}
